﻿using LiteBlog.IoC;
using LiteBlog.Repositories.Common;
using LiteBlog.Repositories.Contracts;
using LiteBlog.Repositories.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Xml.Linq;

namespace LiteBlog.Repositories.Tests
{
    [TestClass]
    public abstract class LoadCompTests<T, U> where U: DataAccess, ILoadableRepository<T>
    {
        protected Mock<IXmlHelper> _mock;
        protected ILoadableRepository<T> _comp;
        protected U _data;

        [TestInitialize]
        public void InitTest()
        {
            _comp = _data = IoCContainer.Resolve<U>();
            _mock = Mock.Get<IXmlHelper>(_data.XmlHelper);
        }

        [TestMethod]
        public void LoadComponentIsNotNull()
        {
            Assert.IsNotNull(_comp);
        }

        [TestMethod]
        public void LoadWorks()
        {
            SetupLoad();
            _comp.Load();
            _mock.Verify(x => 
                x.Load(It.Is<string>(p => 
                    p.Equals(_data.Path))));
        }

        [TestMethod]
        public void SaveWorks()
        {
            var item = SetupSave();
            _comp.Save(item);
            _mock.Verify(x => x.Save(It.Is<XElement>(elem => VerifySave(elem)),
                                     It.Is<string>(p => p.Equals(_data.Path))));
        }

        protected abstract void SetupLoad();
        protected abstract T SetupSave();
        protected abstract bool VerifySave(XElement root);
    }
}
